package com.android.common.utils;

import android.content.SharedPreferences;
import com.android.common.constant.Constant;

public class SpUtils {

    public static String SP_FILE_NAME = "APP_SP";

    /**
     * 存储字符串
     * */
    public static boolean putString(String key, String value) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.putString(key, value);
        return editor.commit();
    }
    /**
     * 读取字符串
     * */
    public static String getString(String key) {
        return getString(key, "");
    }
    /**
     * 读取字符串（带默认值的）
     * */
    public static String getString(String key, String defaultValue) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        return preferences.getString(key, defaultValue);
    }
    /**
     * 存储整型数字
     * */
    public static boolean putInt(String key, int value) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.putInt(key, value);
        return editor.commit();
    }
    /**
     * 读取整型数字
     * */
    public static int getInt(String key) {
        return getInt(key, -1);
    }
    /**
     * 读取整型数字（带默认值的）
     * */
    public static int getInt(String key, int defaultValue) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        return preferences.getInt(key, defaultValue);
    }
    /**
     * 存储长整型数字
     * */
    public static boolean putLong(String key, long value) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.putLong(key, value);
        return editor.commit();
    }
    /**
     * 读取长整型数字
     * */
    public static long getLong(String key) {
        return getLong(key, 0xffffffff);
    }
    /**
     * 读取长整型数字（带默认值的）
     * */
    public static long getLong(String key, long defaultValue) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        return preferences.getLong(key, defaultValue);
    }
    /**
     * 存储Float数字
     * */
    public static boolean putFloat(String key, float value) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.putFloat(key, value);
        return editor.commit();
    }
    /**
     * 读取Float数字
     * */
    public static float getFloat(String key) {
        return getFloat(key, -1.0f);
    }
    /**
     * 读取Float数字（带默认值的）
     * */
    public static float getFloat(String key, float defaultValue) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        return preferences.getFloat(key, defaultValue);
    }
    /**
     * 存储boolean类型数据
     * */
    public static boolean putBoolean(String key, boolean value) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * 读取boolean类型数据
     * */
    public static boolean getBoolean(String key) {
        return getBoolean(key, false);
    }
    /**
     * 读取boolean类型数据（带默认值的）
     * */
    public static boolean getBoolean(String key, boolean defaultValue) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        return preferences.getBoolean(key, defaultValue);
    }

    /**
     * 删除一条数据
     * */
    public static boolean remove(String key) {
        SharedPreferences preferences = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        return editor.commit();
    }
    /**
     * 清除数据
     * */
    public static boolean clearPreferences() {
        SharedPreferences pref = Constant.GLOBAL_APPLICATION.getSharedPreferences(SP_FILE_NAME, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        return editor.commit();
    }


}
